﻿$(document).ready(function(){


    /*-----------------获取关键词js---------------------*/
    var textb=$("#search");
    var timer=null;//搜索框防抖
    textb.keyup(function(event){
        clearTimeout(timer);

        if(textb.val()==""||textb.val()==" "){
            $("#search-btn").fadeOut(500); 
            var keywordbox=$("#search-keyword-box");//关键词盒子
            keywordbox.animate({
                height:0//关键盒子收缩效果
            },100,function(){
                keywordbox.css({display:"none",height:"auto"});
                keywordbox.empty();//清空盒子内容
            });
            return;
        }
        if(event.which!=39&&event.which!=40&&event.which!=37&&event.which!=38&&event.which!=13){
            $("#search-menu-box").fadeOut(200);
            $("#search-btn").fadeIn(500); 
            $("#search-wrap").animate({opacity: 1},0);            
            timer=setTimeout(function(){ //搜索框防抖
                toAjax();
            },200);
        }
    });

    function toAjax(){
        $.ajax({
            url:"https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",
            type:"GET",
            dataType:"jsonp",
            jsonp: 'jsoncallback',
            // async: false,//同步
            timeout: 5000,//请求超时
            data:{
                "wd":textb.val(),
                "cb":"keydata"
            },
            success: function (json) {
            },
            error: function (xhr) {
                return;
            }

        });
    }

});



//打印关键词
function keydata(keys){
    

    
        var len=keys.s.length;
        var keywordbox=$("#search-keyword-box");//关键词盒子
        var textb=$("#search");
        var subb=$("#search-btn");
        var oBg=document.getElementById('blackBg');
 
        var searchCheck=document.getElementById('search-form');
    
        if(len==0){
            keywordbox.css({display:"none"});
        }else{
            keywordbox.css({display:"block"});
            if(len>=8){      //限制显示最多6条联想词
                len=8;
            }
        }
        var spans="";
        for(var i=0;i<len;i++)     
        {
            spans+="<div class='search-keyword'>"+keys.s[i]+"</div>";
        }
        keywordbox.html(spans);//把关键词写入关键词盒子
        keywordbox.animate({
            height:(keywordbox.children().height())*len+20//关键词下滑效果
        },100);                        

        //点击候选词汇

        keywordbox.children().click(function(){
            textb.val($(this).html());//选中词汇放入输入框
            keywordbox.animate({
                height:0//关键盒子收缩效果
            },100,function(){
                keywordbox.css({display:"none",height:"auto"});
                keywordbox.empty();//清空盒子内容
            });

            textb.focus();//输入框获取焦点*/
            if (textb[0].value) {
                var searchUrl=textb[0].dataset.engineStart+replaceStr(textb[0].value)+textb[0].dataset.engineEnd;
                window.open(searchUrl);
            }//提交搜索 以及对% #等字符转码
        });

        //提交按钮获取焦点后
        subb.focus(function(){//提交按钮获取焦点后
            keywordbox.animate({
                height:0//关键盒子收缩效果
            },100,function(){
                keywordbox.css({display:"none",height:"auto"});
                keywordbox.empty();//清空盒子内容

            });
        });

        var numspan=0;//用来指定选择候选词（通过方向键改变）
        textb.keydown(function(event){//如果使用回车提交时，关键词盒子也可以自动收缩
            if(event.which==13){
                //$('#blackBg').fadeOut(0);//去掉blackBg
                keywordbox.animate({
                height:0//关键盒子收缩效果
                },100,function(){
                    keywordbox.css({display:"none",height:"auto"});
                    keywordbox.empty();//清空盒子内容
                });
            }
            //按下下方向键
            if(event.which==40){
                if(numspan==len)
                    numspan=0;
                for(var i=0;i<len;i++){
                    if(numspan==i){
                        keywordbox.children().eq(i).css({
                            "filter:":"alpha(opacity=100)",
                            "opacity":"1"
                        });
                    }else{
                        keywordbox.children().eq(i).css({
                            "filter:":"alpha(opacity=60)",
                            "opacity":"0.4"
                        });
                    }
                }
                textb.val(keywordbox.children().eq(numspan).html());
                numspan++;
            }
            //按下上方向键
            if(event.which==38){

                numspan--;
                if(numspan==len)
                    numspan=0;
                for(var i=0;i<len;i++){
                    if(numspan==i){
                        keywordbox.children().eq(i).css({
                            "filter:":"alpha(opacity=100)",
                            "opacity":"1"
                        });
                    }else{
                        keywordbox.children().eq(i).css({
                            "filter:":"alpha(opacity=60)",
                            "opacity":"0.4"
                        });
                    }
                }
                textb.val(keywordbox.children().eq(numspan).html());

            }
        });
        keywordbox.children().mouseover(function(){
            numspan=$(this).index();
            for(var i=0;i<len;i++){
                if(numspan==i){
                    keywordbox.children().eq(i).css({
                        "filter:":"alpha(opacity=100)",
                        "opacity":"1"
                    });
                }else{
                    keywordbox.children().eq(i).css({
                        "filter:":"alpha(opacity=60)",
                        "opacity":"0.4"
                    });
                }
             }
                
        });
}


//特殊字符转义--------------------------
function replaceStr(str){
    str = str.replace(/\%/g, "%25");
    str = str.replace(/\:/g, "%3A");
    str = str.replace(/\//g, "%2F");
    str = str.replace(/\?/g, "%3F");
    str = str.replace(/\=/g, "%3D");
    str = str.replace(/\&/g, "%26");
    str = str.replace(/\+/g, "%2B");
    str = str.replace(/\ /g, "%20");
    str = str.replace(/\#/g, "%23");			    
    return str;
}